# coding=utf-8
# 本题为考试多行输入输出规范示例，无需提交，不计分。
import sys
import copy


def findLongtime(game_map, location, index_i, index_j):
    return 1


def getShortTime(game_map, location):
    score_map = copy.deepcopy(game_map)
    for i in range(len(game_map)):
        for j in range(len(game_map[0])):
            if game_map[i][j] == 1:
                score_map[i][j] = 250000  # unreachable
            else:
                score_map[i][j] = findLongtime(game_map, location, i, j)
    return simpleRes(location)


def simpleRes(location):
    center = [0, 0]
    num = len(location)
    for loc in location:
        center[0] += loc[0]
        center[1] += loc[1]
    center[0], center[1] = round(center[0] / num), round(center[1] / num)
    distance = 0
    for loc in location:
        new_dis = abs(loc[0] - center[0]) + abs(loc[1] - center[1])
        distance = max(distance, new_dis)
    return distance


if __name__ == "__main__":
    # 读取第一行的n
    line = sys.stdin.readline().strip()
    n, m = list(map(int, line.split()))
    game_map = []
    for i in range(n):
        # 读取每一行
        line = sys.stdin.readline().strip()
        # 把每一行的数字分隔后转化成int列表
        values = list(map(int, list(line)))
        game_map.append(values)

    # 读取第二行的n
    n = int(sys.stdin.readline().strip())
    location = []
    for i in range(n):
        # 读取每一行
        line = sys.stdin.readline().strip()
        # 把每一行的数字分隔后转化成int列表
        values = list(map(int, line.split()))
        location.append(values)

    shortime = getShortTime(game_map, location)
    print(shortime)
